package com.Main.faislamovers;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.widget.Toast;


class RequestTask extends AsyncTask<String, String, String>{

	public Context context;
	public RequestTask(android.view.View.OnClickListener onClickListener)
	{
		context = (Context) onClickListener;
		
	}
@Override
	protected void onPreExecute() 
           {

            }
	
    @Override
    protected String doInBackground(String... uri) {
    	
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
        if(result.equals("login failed"))
        {
        Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Response....");
		

		builder.setMessage(result);
		
		builder.setPositiveButton("OK", new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "You Click OK", Toast.LENGTH_SHORT).show();
			}
		});
		AlertDialog Diloge = builder.create();
		Diloge.show();
		
        }
        else
        {
        Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Response....");
	
		StringBuilder Name = new StringBuilder(); 
		JSONArray jObject;
		try {
			jObject = new JSONArray(result);
			Name.append("No of Campaigns "+ jObject.length());
			Name.append("\n");
			Name.append("\n");
			
        for (int i = 0; i < jObject.length(); i++) {
        	
             JSONObject menuObject = jObject.getJSONObject(i);

             String name= menuObject.getString("Name");
             String desc= menuObject.getString("GoalDescription");
             Name.append("Campaign "+(i+1)+" Name is = "+name+"\n");
             Name.append("\t Description is = "+desc+"\n");
         	Name.append("\n");
        }
			} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		builder.setMessage(Name);
		
		builder.setPositiveButton("OK", new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "You Click OK", Toast.LENGTH_SHORT).show();
			}
		});
		AlertDialog Diloge = builder.create();
		Diloge.show();
        
        
        
        }
    }
    
   

}


